import Mustache from 'mustache'
import { getMustacheData } from './handlers'

export const setupTs = async (apiItems: IApiItem[]) => {
    const data = await getMustacheData(apiItems)

    return Mustache.render(
        `import {
	{{#data}}
	{{methodLower}}{{funcName}}Api,
	{{/data}}
} from '@/repository'
import {
	{{#data}}
	{{#funcParamsTs}}
	I{{funcName}}ApiParams,
	{{/funcParamsTs}}
	I{{funcName}}ApiBody,
	{{/data}}
} from '@/repository/types'
import { showToast } from 'vant'
import { ref } from 'vue'

{{#data}}
// {{title}}
const {{funcNameCamel}} = ref<I{{funcName}}ApiBody | null>(null)
{{/data}}

{{#data}}
// 获取{{title}}
const fetch{{funcName}} = async ({{{funcParamsTs}}}) => {
	const { code, data, msg } = await {{methodLower}}{{funcName}}Api({{{funcParamsJs}}})
	if (code !== 0) {
		showToast(msg)
		return null
	}
	{{funcNameCamel}}.value = data
}

{{/data}}
`,
        { data },
    )
}
